﻿using Data.Model.DaMeng;
using DataService.Core.Common;
using DataService.Core.DataHelper;
using DataService.Core.ServiceDTO;
using DataService.Data;
using DataService.Main;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Text;

namespace UnitTest
{
    public class TestDM
    {
        string connStr = "Server=127.0.0.1:5236; UserId=SYSDBA; PWD=SYSDBA;";
        public void TestDMTableSpace()
        {
            using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.DaMeng))
            {
                db.ConnectionString = connStr;
                ModelDAL.CreateDataBaseSchema(@"D:\DMDATA","TestDM",db);
            }
            Console.WriteLine("完成");
        }
        public void CreateTable()
        {
            using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.DaMeng))
            {
                db.ConnectionString = connStr;
                db.TableSuffix = "";
                ModelDAL.CreatTableSchema("PersonnelInfo", db);
                ModelDAL.CreatTableSchema("TestIndexTable", db);
                ModelDAL.CreateModelCodes(@"D:\DataService\UnitTest\Model\", db, "PersonnelInfo", "TB_PersonnelInfo_AchieveInformation");
            }
            Console.WriteLine("达梦自动建表完成");
        }
        public void CreateModel()
        {
            using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.DaMeng))
            {
                db.ConnectionString = connStr;
                db.TableSuffix = "";
                ModelDAL.CreateModelCodes(@"D:\DataService\UnitTest\Model\", db, "PersonnelInfo", "TB_PersonnelInfo_AchieveInformation", "TestIndexTable");
            }
            Console.WriteLine("达梦自动建表完成");
        }


        public void TestDMDBConnection()
        {
            using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.DaMeng))
            {
                string dd = db.ServerName;
                db.ConnectionString = connStr;
                var d1 = db.DbConnection;
                var tran = db.Transaction;

                var dt = d1.GetSchema();

                foreach (DataRow itemRow in dt.Rows)
                {
                    StringBuilder stringBuilder1 = new StringBuilder();
                    foreach (DataColumn itemColumn in dt.Columns)
                    {

                        stringBuilder1.Append($"{itemRow[itemColumn.ColumnName]}");
                    }
                    Console.WriteLine(stringBuilder1.ToString());
                }


                Console.WriteLine(db.User + "***模式名：" + db.Schema + "服务器：" + db.ServerName + "数据库：" + db.DatabaseName);
            }

        }


        public void TestDMBeatchInsert()
        {
            using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.DaMeng))
            {


                db.ConnectionString = connStr;
                //db._IsolationLevel = IsolationLevel.ReadUncommitted;

                TestSqlserver testSqlserver = new TestSqlserver();
                string[] majors = testSqlserver.majors;
                string[] phones = testSqlserver.phones;
                Snowflake snowflake1 = new Snowflake();

                int XingLen = testSqlserver.Xing.Count;//501个姓
                char[] ManChar = testSqlserver._lastNameMan.ToCharArray();//151的男士名
                char[] WoManChar = testSqlserver._lastNameWoMan.ToCharArray();//151个女士名
                int ManNameLen = ManChar.Length;//151的男士名
                int WoManNameLen = WoManChar.Length;//151个女士名
                int tempNo = 0;

                for (int n = 0; n < 5; n++)
                {
                    Guid departmentId = Guid.NewGuid();
                    List<PersonnelInfo> list = new List<PersonnelInfo>();

                    Random random = new Random();
                    int xingRand = random.Next(XingLen);
                    int mingRand = random.Next(ManNameLen);
                    string xm = testSqlserver.Xing[xingRand] + ManChar[mingRand];
                    //  string xm = "十发达斯洛伐克";
                    for (int i = 0; i < 2000; i++)
                    {
                        tempNo = tempNo + 1;
                        string personNo = tempNo.ToString().PadLeft(6, '0');
                        string idNo = "000000000000000000";
                        try
                        {
                            idNo = IDCardNumber.generate();
                        }
                        catch
                        {
                            idNo = "000000000000000000";
                        }
                        Random random1 = new Random();
                        int majorRand = random1.Next(majors.Length);
                        string tempfullMajor = majors[majorRand];
                        majorRand = random1.Next(majors.Length);
                        string partMajor = majors[majorRand];

                        PersonnelInfo testModel = new PersonnelInfo();
                        testModel.ID = snowflake1.NewId(DateTime.Now);
                        testModel.PersonnelName = xm;
                        testModel.PersonnelNo = personNo;
                        testModel.Status = 1;
                        testModel.FullTimeMajor = tempfullMajor;
                        testModel.PartTimeMajor = partMajor;
                        testModel.IDNo = idNo;
                        testModel.Sex = true;
                        testModel.TermOfValidity = DateTime.Now.ToShortDateString();
                        testModel.LearningWay = "全日制";
                        testModel.WorkYear = RandHelper.Next(2, 60);
                        int num = random.Next(7);
                        string tempStr1 = string.Empty;
                        for (int l = 0; l < num; l++)
                        {

                            for (int k = 0; k < 7; k++)
                            {
                                int tempRand = random.Next(ManNameLen);
                                tempStr1 = tempStr1 + ManChar[tempRand];
                            }

                        }

                        testModel.FullTimeUniversity = tempStr1;


                        int num1 = random.Next(7);
                        string tempStr2 = string.Empty;
                        for (int l = 0; l < num1; l++)
                        {

                            for (int k = 0; k < 6; k++)
                            {
                                int tempRand = random.Next(ManNameLen);
                                tempStr2 = tempStr2 + ManChar[tempRand];
                            }

                        }
                        testModel.PartTimeUniversity = tempStr2;
                        int d = RandHelper.Next(111111111, 999999999);

                        int pd = random1.Next(phones.Length);
                        string s = phones[pd];
                        s = s + d.ToString();

                        testModel.Phone = s;

                        int dd = random1.Next(100);
                        testModel.FullTimeGraduationTime = DateTime.Now.AddDays(dd).ToString("yyyy-MM-dd");
                        dd = random1.Next(100);
                        testModel.PartTimeGraduationTime = DateTime.Now.AddDays(dd).ToString("yyyy-MM-dd");
                        dd = random1.Next(100);
                        testModel.SocialLastTime = DateTime.Now.AddDays(dd).ToString("yyyy-MM-dd");
                        testModel.EmployeeType = "在职";

                        int dds = random.Next(6);
                        List<string> tempDatas = new List<string>();
                        for (int l = 0; l < dds; l++)
                        {
                            string tempStr = string.Empty;
                            for (int k = 0; k < 5; k++)
                            {
                                int tempRand = random.Next(ManNameLen);
                                tempStr = tempStr + ManChar[tempRand];
                            }
                            tempDatas.Add(tempStr);
                        }

                        testModel.CertificateNames = string.Join(",", tempDatas);
                        testModel.DepartmentId = departmentId.ToString();

                        list.Add(testModel);
                    }

                    DAL.Insert(list, db);
                    db.Commit();

                }



                Console.WriteLine($"完成， {Thread.CurrentThread.ManagedThreadId}");

            }

        }



        public void ReadData()
        {
            using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.DaMeng))
            {

                db.ConnectionString = connStr;
                int totalCount = 0;
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                //页码，先正向查询，再逆向，再正向
                int[] pageIndexs = new int[] { 1, 2, 6, 8, 2, 3, 7, 1, 5 };
                ResultPageDTO resultPageDTO = new ResultPageDTO();
                foreach (var itemIndex in pageIndexs)
                {

                    RequestPageDTO paramDTO = new RequestPageDTO
                    {
                        PageIndex = itemIndex,
                        PageSize = 20,
                        ParamNextStartId = resultPageDTO.ParamNextStartId,
                        ParamUpStartId = resultPageDTO.ParamUpStartId,
                        UniqueKey = resultPageDTO.UniqueKey
                    };
                    //  db.TableSuffix = "_20221127";
                    // var tempArray = DAL.QueryPage<TB_PersonnelInformation>(x =>true&&x.PersonnelName.Contains("第1000批次"),30, 499999, null, null, out recordCount, db,t=>new {t.ID,t.PersonnelNo,t.PersonnelName });

                    //&& x.PersonnelName== "劳光"
                    string[] ffff = new string[] { "测试1", "测试2", "测试3" };
                    //x => true&& x.DepartmentId!= ""&&x.PersonnelNo=="ddd"&&x.PersonnelName=="aaa"&&x.IsDelete
                    var tempArray = DAL.QueryPage<Data.Model.DaMeng.PersonnelInfo>(x => x.PersonnelName == "崔裕", paramDTO, db, ref resultPageDTO, t => new { t.ID, t.PersonnelNo });
                    Console.WriteLine($"第{itemIndex}页返回的唯一标识：{resultPageDTO.UniqueKey}，最大Id：{resultPageDTO.ParamNextStartId},最小Id{resultPageDTO.ParamUpStartId}");
                    Console.WriteLine($"****************************第{itemIndex}页*******************************");
                    var tempList = tempArray.ToList();
                    foreach (var item in tempList)
                    {
                        Console.WriteLine(item.ID + "**" + item.PersonnelName + "***" + item.PersonnelNo + "**" + item.AuditState);
                    }
                    Console.WriteLine($"****************************第{itemIndex}页*******************************");
                    totalCount = resultPageDTO.TotalCount;
                }
                stopwatch.Stop();
                Console.WriteLine($"读取数据流耗时：{stopwatch.ElapsedMilliseconds}；总行数：{totalCount}");
                stopwatch.Restart();

                stopwatch.Stop();



                Console.WriteLine("完成");


            }

        }

    }
}
